plunderHandler = plunderHandler or {};
local this = plunderHandler
require "proto/player/message/ReqChangePlunderFormationMessage"
-- require "proto/player/message/ReqPlunderSearchMessage" 
require "proto/player/message/ReqPlunderTargetInfoMessage"
require "proto/player/message/ReqPlunderBattleMessage"
require "proto/player/message/ReqBuyPlunderItemMessage"
require "proto/player/message/ReqPlunderHistorysMessage"
require "proto/player/message/ReqPlunderInfoMessage"
require "proto/player/message/ReqGetPlunderRewardMessage"
require "proto/player/message/ReqChangePlunderFunctionMessage"

-- 返回设置阵容
plunderHandler.cur_def_formation = {}
function plunderHandler.receiveChangePlunderFormation(msg)
    if msg and msg.formation then
        for i = 1, 6 do
            plunderHandler.cur_def_formation[i] = msg.formation[i]
        end
    end
    FormationManager.SetFormation(EnumConst.FormationType.PlunderBattle, plunderHandler.cur_def_formation)
end

-- 返回搜索
-- function plunderHandler.receivePlunderSearch(msg) 
--     if isOpenUI(PanelNames.UIBigMap) then
--         local ui_big_map = CtrlManager.GetCtrl(PanelNames.UIBigMap) 
--         ui_big_map.ResPlunderSearch(msg)
--     end
-- end

-- 返回掠夺列表数据
---@type PlunderBriefPlayerInfo[]
plunderHandler.list_data = {}
plunderHandler.new_list_data = {}
---@deprecated 已废弃
function plunderHandler.receivePlunderListData(msg)
    if true then
        printError("该协议已废弃 "..msg.msgId)
        return
    end
    plunderHandler.list_data = {}
    plunderHandler.new_list_data = plunderHandler.new_list_data or {}
    plunderHandler.show_index = 1
    if msg and msg.targetList then
        for _, v in pairs(msg.targetList) do
            local power = tonumber(tostring(v.power) )
            if power > 0 then
                local item = {}
                item.playerId = tostring(v.playerId)
                item.name = v.name or "-"
                item.pic = v.pic or ""
                item.picFrame = v.picFrame or ""
                item.lv = v.lv or 1
                item.power = power or 0
                item.total_power = v.totalPower and tonumber(tostring(v.totalPower)) or 0
                item.settle_time = v.settleTime and tonumber(tostring(v.settleTime)) or 0
                item.targetId = v.targetId or 0
                item.total_plunder_val = v.totalPlunderVal or 0 
                item.red_val = v.redVal or 0
                -- item.flag = plunderHandler.new_list_data[item.playerId] and false or true
                -- plunderHandler.new_list_data[player_id] = true
                table.insert(plunderHandler.list_data, item)
            end
        end
        -- if plunderHandler.have_search ~= nil then
            plunderHandler.sendReqPlunderSearch()
        -- end
    end
end

plunderHandler.is_rob = nil
-- 返回对手详情
plunderHandler.is_revenge = nil
function plunderHandler.receivePlunderTargetInfo(msg) 
    if plunderHandler.is_revenge then
        plunderHandler.is_revenge = nil
        if isOpenUI(PanelNames.UIRevengeRecord) then
            local ui_revenge_record = CtrlManager.GetCtrl(PanelNames.UIRevengeRecord) 
            ui_revenge_record.ResRevengeMessage(msg)
        end 
    else
        if isOpenUI(PanelNames.UIRob) then
            local ui_rob = CtrlManager.GetCtrl(PanelNames.UIRob) 
            ui_rob.ResUpdatePlayerInfo(msg)
        end
    end 
end

-- 返回对战
function plunderHandler.receivePlunderBattle(msg) 
    if msg.state ~= 0 then
        logError("进入战斗错误-战斗返回失败 code:" .. msg.state)
        FireEvent(Config.EventType.LoadingCancelByFightError, "战斗返回失败")
        return
    end

    -- 埋点
    if plunderHandler.is_revenge_dot then 
        plunderHandler.Track_plunderRevenge(msg)
    else
        plunderHandler.Track_plunderStart(msg)
    end
    plunderHandler.is_revenge_dot = nil
  
    logicMgr.BattlePlaybackManager.ResetReceiveReport()
    BattlePlaybackManager.GotoFight(EnumConst.FightTypeConst.Plunder)
    FightManager.InitRewards(msg.items) 
    FightManager.InitExtraRewards(msg.winReward) 
    logicMgr.BattlePlaybackManager.RequestReport(msg.fightId, msg.fightResult, msg.record)

    ------------------------------------------------

end

-- 返回购买掠夺令
function plunderHandler.receiveBuyPlunderItem(msg) 
    local new_count = plunderHandler.cur_buy_plunder_count+1
    plunderHandler.SetBuyPlunderItemCount( new_count ) 

    if isOpenUI(PanelNames.UIBigMap) then
        local ui_big_map = CtrlManager.GetCtrl(PanelNames.UIBigMap) 
        ui_big_map.ResAddPlunderItem(msg)
    end
    if isOpenUI(PanelNames.UIRob) then
        local ui_rob = CtrlManager.GetCtrl(PanelNames.UIRob) 
        ui_rob.ResAddPlunderItem(msg)
    end 
end
 

 -- 返回掠夺与复仇列表
function plunderHandler.receiveRevengeList( msg )    
    if msg and msg.type then 
        if msg.type == 1 then  
            plunderHandler.rob_receive_reward = msg.list or {} 
            FireEvent(Config.EventType.Fresh_WorldMapReceiveRobReward)
        end 
        if isOpenUI(PanelNames.UIRevengeRecord) then
            local ui_revenge_record = CtrlManager.GetCtrl(PanelNames.UIRevengeRecord) 
            ui_revenge_record.ResUpdateRobRecordInfo(msg)
        end 

    end
end

 -- 返回掠夺记录列表
-- function plunderHandler.receiveRobDataList( msg ) 
--     print("receiveRobDataList----------------------->", table.tostring(msg) )
--     if isOpenUI(PanelNames.UIRevengeRecord) then
--         local ui_rob_record = CtrlManager.GetCtrl(PanelNames.UIRobRecord) 
--         ui_rob_record.ResUpdateRobRecordInfo(msg)
--     end 
-- end

plunderHandler.change_plunder_function_type = 1
function plunderHandler.receiveChangePlunderFunction( msg ) 
    if msg and msg.type then
        plunderHandler.change_plunder_function_type = msg.type
        plunderHandler.last_change_func_time = Config.serverTime
        this.InitRobList(msg.targetList)
        FireEvent(Config.EventType.Refresh_ChangePlunderFunction)
        if isOpenUI(PanelNames.UIBigMap) then
            local ui_big_map = CtrlManager.GetCtrl(PanelNames.UIBigMap) 
            ui_big_map.ResUpdateOnOff()
        end
        plunderHandler.Track_plunderChangeType(msg.type)
    end
end

-- 请求掠夺功能开关
function plunderHandler.sendReqChangePlunderFunction( _type ) 
    local req = ReqChangePlunderFunctionMessage.New() 
    req.type = _type or 0
    sendReqMessage(req)
end

-- 请求玩家掠夺数据返回
---@param msg ResPlunderInfoMessage
function plunderHandler.receivePlunderInfo(msg) 
    if msg and msg.defFormation  then 
        for i=1, 6 do
            plunderHandler.cur_def_formation[i] = msg.defFormation[i]
        end 
    end
    if msg and msg.atkFormation then
        plunderHandler.SetAtkFomations( msg.atkFormation )
    end
    if msg and msg.buyItemTimes then
        plunderHandler.SetBuyPlunderItemCount( msg.buyItemTimes )
    end
    if msg and msg.totalPlunderTime then
        plunderHandler.SetTodayRobAllTime( tonumber(tostring(msg.totalPlunderTime) ) )  
    end

    if msg and msg.plunderTimes then
        plunderHandler.SetTodayFreeRobTimes( msg.plunderTimes )  
    end
    
    if msg and msg.lastChangeFunctionTime then
        plunderHandler.last_change_func_time = tonumber(tostring(msg.lastChangeFunctionTime))
    end
    if msg and msg.functionOpen then
        plunderHandler.change_plunder_function_type = tonumber(msg.functionOpen) or 1
        if isOpenUI(PanelNames.UIBigMap) then
            local ui_big_map = CtrlManager.GetCtrl(PanelNames.UIBigMap) 
            ui_big_map.ResUpdateOnOff()
        end
    end
    plunderHandler.list_data = {}
    plunderHandler.new_list_data = plunderHandler.new_list_data or {}
    plunderHandler.show_index = 1
    this.InitRobList(msg.targetList)
        -- if plunderHandler.have_search ~= nil then
        --plunderHandler.sendReqPlunderSearch()
        -- end
    FireEvent(Config.EventType.Refresh_PlunderInfo)
    if isOpenUI(PanelNames.UIBigMap) then
        local ui_big_map = CtrlManager.GetCtrl(PanelNames.UIBigMap) 
        ui_big_map.ResPlunderInfo(msg)
    end
end
function plunderHandler.InitRobList(targetList)
    if not targetList then return end
    table.clear(this.list_data)
    for _, v in pairs(targetList) do
        local power = tonumber(tostring(v.power) )
        if power > 0 then
            local item = {}
            item.playerId = tostring(v.playerId)
            item.name = v.name or "-"
            item.pic = v.pic or ""
            item.picFrame = v.picFrame or ""
            item.lv = v.lv or 1
            item.power = power or 0
            item.total_power = v.totalPower and tonumber(tostring(v.totalPower)) or 0
            item.settle_time = v.settleTime and tonumber(tostring(v.settleTime)) or 0
            item.targetId = v.targetId or 0
            item.total_plunder_val = v.totalPlunderVal or 0
            item.red_val = v.redVal or 0
            table.insert(this.list_data, item)
        end
    end
end
plunderHandler.last_change_func_time = 0

function plunderHandler.SetTodayRobAllTime( total_time )  
   plunderHandler.rob_total_time = total_time
end

plunderHandler.today_free_rob_times = 0
function plunderHandler.SetTodayFreeRobTimes( total_time )  
   plunderHandler.today_free_rob_times = total_time
   if isOpenUI(PanelNames.UIRob) then
        local ui_rob = CtrlManager.GetCtrl(PanelNames.UIRob) 
        ui_rob.UpdateFreeRobNum() 
    end 
end

-- 领取掠夺奖励请求
function plunderHandler.sendReqGetPlunderRewardMessage( history_id )  
   local req = ReqGetPlunderRewardMessage.New() 
   req.historyId = history_id
   -- print("------------------->", history_id)
   sendReqMessage(req)
end

-- 领取掠夺奖励返回
function plunderHandler.receiveReqGetPlunderRewardMessage( msg )  
    -- print("receiveReqGetPlunderRewardMessage------------------->", table.tostring(msg) )
    if isOpenUI(PanelNames.UIRevengeRecord) then
        local ui_rob_record = CtrlManager.GetCtrl(PanelNames.UIRevengeRecord) 
        ui_rob_record.ResPlunderRobReward(msg)
        plunderHandler.Track_plunderReceiveReward(msg)
    end 
end
 
plunderHandler.cur_buy_plunder_count = 0
function plunderHandler.SetBuyPlunderItemCount( count )
    plunderHandler.cur_buy_plunder_count = count or 0
end

function plunderHandler.receiveBePlunderNotice(msg) 
    plunderHandler.SetRevengeListRedDotData( true )
end


-- 资源掠夺请求
--设置阵容
function plunderHandler.sendReqChangePlunderFormation( formation )  
    local req = ReqChangePlunderFormationMessage.New() 
    req.formation = formation
    sendReqMessage(req)
end

--搜索
plunderHandler.show_num = nil
plunderHandler.show_index = 1
plunderHandler.have_search = nil
---@deprecated 已废弃
function plunderHandler.sendReqPlunderSearch(reset)
    if true then
        printError("已废弃")
        return
    end
    --if isOpenUI(PanelNames.UIBigMap) then
        local list_data = {}
        if plunderHandler.show_num == nil then
            plunderHandler.show_num = dataTable.getDataTableByStringId("t_global", 329 ,"f_int_value")
        end
        if plunderHandler.list_data == nil then
            plunderHandler.list_data = {}
        end
        local data_len = #plunderHandler.list_data
        if reset then
            this.show_index = 1
        end
        if plunderHandler.show_index > data_len then
            plunderHandler.show_index = 1
        end 
        local count = 0
        for i=1, plunderHandler.show_num do
            local item = plunderHandler.list_data[plunderHandler.show_index+i-1]
            if item ~= nil then
                table.insert(list_data, item)
                count = count + 1
            end
        end 
        plunderHandler.show_index = plunderHandler.show_index + count
    this.cur_search_list_data = list_data
    FireEvent(Config.EventType.Refresh_PlunderInfo)


    if isOpenUI(PanelNames.UIBigMap) then
        local ui_big_map = CtrlManager.GetCtrl(PanelNames.UIBigMap)
        ui_big_map.ResPlunderSearch(list_data)
    end
    --end
end
function plunderHandler.IsOpen()
    return this.change_plunder_function_type == 1
end
---@type PlunderBriefPlayerInfo[]
plunderHandler.cur_search_list_data = nil
---@return PlunderBriefPlayerInfo[]
function plunderHandler.GetCurPlunderSearchData()
    return this.list_data
    --[[if this.cur_search_list_data and next(this.cur_search_list_data) ~= nil then
        return this.cur_search_list_data
    end
    local list_data = {}
    if this.show_num == nil then
        this.show_num = dataTable.getDataTableByStringId("t_global", 329 ,"f_int_value")
    end
    if this.list_data == nil then
        this.list_data = {}
    end
    local data_len = #this.list_data
    if this.show_index > data_len then
        this.show_index = 1
    end 
    local count = 0
    for i=1, this.show_num do
        local item = this.list_data[this.show_index+i-1]
        if item ~= nil then
            table.insert(list_data, item)
            count = count + 1
        end
    end
    this.cur_search_list_data = list_data
    return list_data]]
end

--对手详情
function plunderHandler.sendReqPlunderTargetInfo( player_id, target_id ) -- player_id 玩家id
    -- print("plunderHandler.sendReqPlunderTargetInfo------------------>", player_id, target_id)
    local req = ReqPlunderTargetInfoMessage.New() 
    req.playerId = player_id
    req.targetId = target_id or 0
    sendReqMessage(req)
end
 
--对战
function plunderHandler.sendReqPlunderBattle( data ) --对战
    local req = ReqPlunderBattleMessage.New() 
    req.playerId = data.playerId
    data.formation = data.formation or {}
    for i=1, 6 do
        if data.formation[i] == nil then
            data.formation[i] =0
        end
    end
    req.formation = data.formation 
    req.type = data.type
    req.historyId = data.historyId 
    req.targetId = data.targetId
    sendReqMessage(req)
end

function plunderHandler.SetAtkFomations( formation )
    if formation then
        for k, v in ipairs(formation) do
            formation[k] = tostring(v) 
        end
    end
    plunderHandler.atk_formation = formation or {}
end

--购买掠夺令
function plunderHandler.sendReqBuyPlunderItem() 
    local req = ReqBuyPlunderItemMessage.New() 
    sendReqMessage(req)
end

-- 掠夺记录列表
function plunderHandler.sendReqRobList( _type ) --类型 1掠夺与被复仇 2被掠夺
    local req = ReqPlunderHistorysMessage.New() 
    req.type = _type 
    sendReqMessage(req)
end
 
-- plunderHandler.show_pool = 1
-- 请求玩家掠夺数据
function plunderHandler.sendReqPlunderInfo()
    -- 太多地方调用了，就在这判断吧
    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Rob,false) then
        return;
    end
    local req = ReqPlunderInfoMessage.New() 
    req.showPool = 1 --plunderHandler.show_pool
    sendReqMessage(req)
    -- if plunderHandler.show_pool == 1 then
    --     plunderHandler.show_pool = 0
    -- end
end

function plunderHandler.SetItemInfoFlagState( player_id, state_flag )
    if player_id then
        -- local list_data = plunderHandler.list_data or {}
        -- for k, v in pairs(list_data) do
        --     if player_id == v.playerId then
        --         plunderHandler.list_data[k].flag = state_flag
        --     end
        -- end
        plunderHandler.new_list_data[player_id] = state_flag
    end
end

function plunderHandler.ResetData()
    plunderHandler.new_list_data = {}
    plunderHandler.have_search = nil
    plunderHandler.is_help = nil
    -- plunderHandler.show_pool = 1
    plunderHandler.today_free_rob_times = 0
	plunderHandler.revenge_red_dot = false
    plunderHandler.help_plunder_list_data_map = {}
    plunderHandler.cur_help_plunder_reddot = 0
    plunderHandler.can_help_plunder_count = 0
    plunderHandler.new_plunder_list_data = {}
    plunderHandler.union_help_memeber_id = nil
    plunderHandler.union_help_attack_id = nil
end

function plunderHandler.GetItemInfoFlagState( player_id )
    if player_id and  plunderHandler.new_list_data[player_id] then 
        return false
    end
    return true
end

plunderHandler.rob_item_data = nil
---战斗完更新
function plunderHandler.UpdateRobListData() 
    if plunderHandler.rob_item_data ~= nil then
        local player_id = plunderHandler.rob_item_data
        local list_data = plunderHandler.list_data
        plunderHandler.list_data = {}
        if list_data ~= nil then
            for _, v in pairs(list_data) do 
                if v.playerId ~= player_id then
                    table.insert(plunderHandler.list_data, v)
                end
            end
        end

        local list_data2 = plunderHandler.cur_search_list_data or {}
        plunderHandler.cur_search_list_data = {}
        for _, v in pairs(list_data2) do
            if v.playerId ~= player_id then
                table.insert(plunderHandler.cur_search_list_data, v)
            end
        end
        plunderHandler.rob_item_data = nil
    end
end

function plunderHandler.GetRobDataByid(pid)
    local list_data = plunderHandler.list_data or {}
    for k,v in pairs(list_data) do
        if tostring(v.playerId) == tostring(pid) then
            return v
        end
    end
    return nil
end

plunderHandler.atk_target_name = nil
plunderHandler.revenge_red_dot = false
function plunderHandler.SetRevengeListRedDotData( red_dot )
    if red_dot then
        plunderHandler.revenge_red_dot = true
    else
        plunderHandler.revenge_red_dot = false
    end  
    FireEvent(Config.EventType.Fresh_WorldMapRevengeList)
end
function plunderHandler.GetRevengeListRedDotData()  
    return plunderHandler.revenge_red_dot
end
 
function plunderHandler.GetRobReceiveRewardRedDotData()  
    if plunderHandler.rob_receive_reward ~= nil and next(plunderHandler.rob_receive_reward) ~= nil then
        local rob_record_over_time = dataTable.getDataTableByStringId("t_global", 336 ,"f_int_value") 
        for _, v in pairs(plunderHandler.rob_receive_reward) do  
            if v.type == 3 and (tonumber(tostring(v.createTime))  + rob_record_over_time) <= Config.serverTime then 
                return true 
            end
        end  
    end
    return false
end

--------掠夺埋点 start
--点击掠夺
function plunderHandler.Track_clickPlunder(pid)
    TAManager.TrackByDictionary("click_plunder", {player_id = tostring(pid)})
end

--进行掠夺
function plunderHandler.Track_plunderStart(msg)
    local data = {}
    data.player_id       = tostring(msg.playerId)         --目标玩家id
    data.success         = msg.fightResult          --是否成功0否1是
    data.reward_time     = math.floor(tonumber(tostring(msg.rewardTime)) / 1000)
    data.my_power        = tostring(msg.record.attacker.power)
    data.other_power     = tostring(msg.record.defender.power)
    data.all_time        = math.floor(data.reward_time * msg.plunderPer / 100)        --总计掠夺挂机时间

    local cur_total_time = plunderHandler.rob_total_time 
    local up_time = dataTable.getDataTableByStringId("t_global", 321 ,"f_int_value") 
    data.is_max          = plunderHandler.rob_total_time >= tonumber(up_time) and 1 or 0
    TAManager.TrackByDictionary("plunder_start", data)
end

--点击复仇
function plunderHandler.Track_clickRevenge(pid)
    TAManager.TrackByDictionary("click_plunder_revenge", {player_id = tostring(pid)})
end

--进行复仇
function plunderHandler.Track_plunderRevenge( msg )
    local data          = {}
    data.playerId       = tostring(msg.playerId) --目标玩家id 
    data.success        = msg.fightResult  
    data.reward_time    = math.floor(tonumber(tostring(msg.rewardTime)) / 1000)
    data.my_power       = tostring(msg.record.attacker.power)
    data.other_power    = tostring(msg.record.defender.power)
    data.all_time       = math.floor(data.reward_time * msg.plunderPer / 100) 

    TAManager.TrackByDictionary("plunder_revenge_start", data)
end

function plunderHandler.Track_plunderClickReward(info)
    -- TAManager.TrackByDictionary("plunder_click_reward", {player_id = tostring(info.playerId)})
end

function plunderHandler.Track_plunderReceiveReward(data)
    local dic = {}
    local server_time   = HelperFunc.GetServerTime(false)
    dic.all_time        = math.floor((server_time - tonumber(tostring(data.createTime or server_time))) * 0.01)         --总计掠夺挂机时间
    dic.player_id       = tostring(data.playerId)

    TAManager.TrackByDictionary("plunder_receive_reward", dic)
end

function plunderHandler.Track_plunderChangeType(type)
    TAManager.TrackByDictionary("plunder_change_type", {type = type})
end
--------掠夺埋点 end

---------------------------------------------------------------------------
--- 战团突袭求助
---------------------------------------------------------------------------
require "proto/unionhelp/message/ReqUnionHelpInfoMessage"
require "proto/unionhelp/message/ReqBeginHelpMessage"
require "proto/unionhelp/message/ReqReadyHelpMessage"

plunderHandler.help_plunder_list_data_map = {}
plunderHandler.cur_help_plunder_reddot = 0
plunderHandler.can_help_plunder_count = 0
plunderHandler.new_plunder_list_data = {}
plunderHandler.is_player_login = true
plunderHandler.is_help = nil

function plunderHandler.ResetUnionHelpData()
    plunderHandler.help_plunder_list_data_map = {}
    plunderHandler.cur_help_plunder_reddot = 0
    plunderHandler.can_help_plunder_count = 0
    plunderHandler.new_plunder_list_data = {}
    plunderHandler.is_player_login = true
    plunderHandler.is_help = nil
    FireEvent(Config.EventType.Fresh_GuildHelpRob)
end

function plunderHandler.SendGetUnionHelpInfo( page_id )
    local req = ReqUnionHelpInfoMessage.New() 
    req.pageId = page_id or 1
    sendReqMessage(req)
end

--应答求助列表数据
function plunderHandler.receiveUnionHelpInfo( msg )
    if msg and msg.state == 0 then
        plunderHandler.new_plunder_list_data = msg.helps or {}
        plunderHandler.can_help_plunder_count = msg.curNum or 0
        plunderHandler.help_plunder_list_data_map = {}
        for _, v in pairs(plunderHandler.new_plunder_list_data) do
            plunderHandler.help_plunder_list_data_map[tostring(v.historyId)] = v.type
        end
        if plunderHandler.is_player_login then
            plunderHandler.is_player_login = false;
            plunderHandler.refreshPlunderReddotsData()
        end
        FireEvent(Config.EventType.UpdateGuildPlunderHelp)
    end
end

function plunderHandler.refreshPlunderReddotsData()
    plunderHandler.cur_help_plunder_reddot = 0
    for _, v in pairs(plunderHandler.help_plunder_list_data_map) do
        if v ~= nil and v == 1 then
            plunderHandler.cur_help_plunder_reddot = 1
            break
        end
    end
    FireEvent(Config.EventType.Fresh_GuildHelpRob)
end
-- 数据变更标记推送
function plunderHandler.receiveUnionHelpInfoStateChange( msg )
    if msg and msg.state == 0 then
        if plunderHandler.help_plunder_list_data_map == nil then
            plunderHandler.help_plunder_list_data_map = {}
        end
        if msg.type == 3 then
            plunderHandler.help_plunder_list_data_map[tostring(msg.historyId)] = nil
            local list_data = plunderHandler.new_plunder_list_data or {}
            plunderHandler.new_plunder_list_data = {}
            for _, v in pairs(list_data) do
                if msg.historyId ~= v.historyId then
                    table.insert(plunderHandler.new_plunder_list_data, v)
                end
            end 
        else
            plunderHandler.help_plunder_list_data_map[tostring(msg.historyId)] = msg.type
        end
        plunderHandler.refreshPlunderReddotsData()
        FireEvent(Config.EventType.UpdateGuildPlunderHelp)
    end
end

-- 获取突袭状态
function plunderHandler.GetUnionHelpPlunderState( history_id )
    if plunderHandler.help_plunder_list_data_map[tostring(history_id)] ~= nil then
        return plunderHandler.help_plunder_list_data_map[tostring(history_id)]
    end
    return 1
end

-- 请求开始求助
-- data.playerId = msg.info.playerId
-- data.formation = formation
-- data.type = 3
-- data.historyId = this.historyId or 0
-- data.targetId = 0
function plunderHandler.SendBeginHelpRequest( data )
    -- print("plunderHandler.SendBeginHelpRequest", table.tostring(data) )
    local req = ReqBeginHelpMessage.New() 
    req.historyId = data.historyId or "0"
    -- req.playerId = playerId
    req.formation = data.formation
    sendReqMessage(req)
end

function plunderHandler.receiveResBeginHelp( msg )
    -- print("plunderHandler.receiveResBeginHelp", table.tostring(msg) )
    if msg.state ~= 0 then
        logError("进入战斗错误-战斗返回失败 code:" .. msg.state)
        FireEvent(Config.EventType.LoadingCancelByFightError, "战斗返回失败")
        return
    end 
  
    logicMgr.BattlePlaybackManager.ResetReceiveReport()
    BattlePlaybackManager.GotoFight(EnumConst.FightTypeConst.Plunder)
    FightManager.InitRewards(msg.items) 
    FightManager.InitExtraRewards(msg.winReward) 
    logicMgr.BattlePlaybackManager.RequestReport(msg.fightId, msg.fightResult, msg.record)

    plunderHandler.UnionHelpResultCollectData()
end

-- 战团突袭助阵埋点
plunderHandler.union_help_memeber_id = nil
plunderHandler.union_help_attack_id = nil
function plunderHandler.UnionHelpResultCollectData()
    local data           = {}
    data.cur_num         = tostring(plunderHandler.can_help_plunder_count)
    data.help_member_id  = tostring(plunderHandler.union_help_memeber_id)
    data.help_attack_id  = tostring(plunderHandler.union_help_attack_id)
    TAManager.TrackByDictionary("union_plunder_help", data)
    plunderHandler.union_help_memeber_id = nil
    plunderHandler.union_help_attack_id = nil
end

-- 拉取阵容信息
function plunderHandler.SendReqReadyHelpMessage( history_id )
    -- print("plunderHandler.SendReqReadyHelpMessage->", history_id)
    local req = ReqReadyHelpMessage.New() 
    req.historyId = history_id or 0
    sendReqMessage(req)
end

--返回玩家阵容
function plunderHandler.receiveResReadyHelp( msg )
    -- print("plunderHandler.receiveResReadyHelp", table.tostring(msg) )
    if msg.state == 0 then
        if isOpenUI(PanelNames.UIGuildRescue) then
            local ui_guild_rescue = CtrlManager.GetCtrl(PanelNames.UIGuildRescue) 
            ui_guild_rescue.ResRevengeMessage(msg)
        end
    end 
end

---------------------------------------------------------------------------
---end
---------------------------------------------------------------------------
---@class PlunderBriefPlayerInfo
---@field
---@field playerId string
---@field name string
---@field pic string
---@field picFrame number
---@field lv number
---@field power number
---@field total_power number
---@field settle_time number
---@field targetId number
---@field total_plunder_val number
---@field red_val number